//
//  GYOrderIngetralCell.m
//  GoodLuck
//
//  Created by apple on 2018/3/20.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

static NSString *integralCellIdentifier = @"integralCellIdentifier";

#import "GYOrderIngetralCell.h"

@interface GYOrderIngetralCell ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UILabel *ingetralSumLable;

@property (weak, nonatomic) IBOutlet UILabel *integralMarkLable;
@property (weak, nonatomic) IBOutlet UITextField *integralTF;

/** 输入显示积分lable */
@property (nonatomic, strong) UILabel *intergralLable;

@property (weak, nonatomic) IBOutlet UILabel *integralMessageLable;

@end
@implementation GYOrderIngetralCell

- (void)awakeFromNib {
    [super awakeFromNib];
    [self.integralTF addTarget:self action:@selector(registerFirst) forControlEvents:UIControlEventEditingDidEndOnExit];
    self.integralMarkLable.hidden = YES;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changedTF:) name:UITextFieldTextDidChangeNotification object:nil];
    
    //设置integralTF发inputView;
    UILabel *integralLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 30)];
    integralLable.backgroundColor = [UIColor whiteColor];
    self.integralTF.inputAccessoryView = integralLable;
    integralLable.text = self.integralTF.text;
    self.intergralLable = integralLable;
}

- (void)registerFirst {
    [self.integralTF resignFirstResponder];
}

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    GYOrderIngetralCell *cell = [tableView dequeueReusableCellWithIdentifier:integralCellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil] firstObject];
    }
    
    return cell;
}

- (CGFloat)height {
    
    [self layoutIfNeeded];
    
    return CGRectGetMaxY(self.integralMarkLable.frame);
}

- (void)setMember_points:(int)member_points {
    _member_points = member_points;
    
    self.ingetralSumLable.text = [NSString stringWithFormat:@"当前可用积分为%d积分,请输入本次使用积分:",member_points];
}

- (void)setSetuppoints:(int)setuppoints {
    _setuppoints = setuppoints;
    
    self.integralMessageLable.text = [NSString stringWithFormat:@"每%d积分可抵扣1元,这算不足1元的积分将不计入抵扣,也不扣除",setuppoints];
}

- (void)setUpdatePoints:(int)updatePoints {
    _updatePoints = updatePoints;
    
    self.integralTF.text = [NSString stringWithFormat:@"%d",updatePoints];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.integralTF resignFirstResponder];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    self.intergralLable.text = textField.text;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if ([textField.text intValue] > self.member_points) {
        textField.text = [NSString stringWithFormat:@"%d",self.member_points];
    }
    self.ingetralCallBack(textField.text);
}


- (void)changedTF:(NSNotification *)noti {
    //判断是否要显示积分不足的提示语
    if ([self.integralTF.text intValue] > self.member_points) {
        //self.integralTF.text = [NSString stringWithFormat:@"%d",self.member_points];
        self.integralMarkLable.hidden = NO;
    }else {
        self.integralMarkLable.hidden = YES;
    }
    self.intergralLable.text = self.integralTF.text;
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}


@end
